//
//  UserAccountViewModel.m
//  OC-微博
//
//  Created by Apple on 15/11/10.
//  Copyright © 2015年 Apple. All rights reserved.
//

#import "UserAccountViewModel.h"
//视图模型
//归解档路径
@interface UserAccountViewModel()
@property (nonatomic, copy) NSString *accountPath;
//判断日期
@property (nonatomic, assign) BOOL isExpired;
@end
@implementation UserAccountViewModel
#pragma mark /*******************  单例  *******************/
//设置单例
+ (instancetype)shardViewModel{
    static  id instance ;
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        instance = [self new];
    });
    return instance;
    
}
#pragma mark /*******************  定义用户头像URL  *******************/
- (NSURL *)avatarUrl {

    if (self.account.avatar_large != nil) {
        
        return [NSURL URLWithString:self.account.avatar_large];
    }
    return nil;
}
#pragma mark /*******************  登录用户标记  *******************/
- (BOOL)userlogin {

    // 1. 如果 token 有值，说明登录成功
    // 2. 如果没有过期，说明登录有效      //感觉后面 isE加上多余，先执行的构造函数，在里面已经判断日期是否过期了
    return self.account.access_token != nil &&  !self.isExpired;
    //简化版 直接判断
//return self.account != nil;
}
#pragma mark /*******************  返回有效token值  *******************/
- (NSString *)token {
    if (!self.isExpired) {
         // 如果 token 没有过期，返回 account.中的 token 属性
        return self.account.access_token;
    }
    return nil;
}
#pragma mark /*******************  判断日期  *******************/
- (BOOL)isExpired {
    if ([self.account.expiresDate compare:[NSDate date]] == NSOrderedDescending) {
        NSLog(@"DATE:%@",self.account.expiresDate);
        return NO;
    }
    //过期返回true
    return YES;
}
#pragma mark /*******************  归解档路径  *******************/
- (NSString *)accountPath {
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"account.plist"];
    return _accountPath = path;

}
- (instancetype)init
{
    self = [super init];
    if (self) {
        _account = [NSKeyedUnarchiver unarchiveObjectWithFile:self.accountPath];
        if (self.isExpired) {
            NSLog(@"日期过期");
           
            //如果过期，恢复的数据设置nil
           
            _account = nil;

        }
        NSLog(@"%@",_account);

    }
    return self;
}
#pragma mark /*******************  抽取用户网络方法  *******************/
- (void)loadAccessTolen:(NSString *)code finished:(void (^)(BOOL))finished {
    // 4. 加载 accessToken
    [[NetworkingTools shardTools]loadAccessToken:code finished:^(id result, NSError *error) {
        // 1> 判断错误
        if (error != nil || result == nil) {
            
            NSLog(@"出错了");
            return;
        }
        //字典转模型
        self.account = [UserAccount initWithdict:result];
        // 2> 输出结果
        [self loadUserInfo:self.account finished:finished];
    }];
 }
     
- (void)loadUserInfo:(UserAccount *)account finished:(void (^)(BOOL))finished {
    [[NetworkingTools shardTools]loadUserInfo:account.uid finished:^(id result, NSError *error) {
        if (error != nil) {
            NSLog(@"加载用户出错了");
            finished(NO);
            return ;
        }
        if (result == nil) {
            NSLog(@"格式错误");
            finished(NO);
            return;
        }
        //保存用户头像和昵称到模型中
        account.screen_name = result[@"screen_name"];
        account.avatar_large = result[@"avatar_large"];
        //保存数据
        [NSKeyedArchiver archiveRootObject:account toFile:self.accountPath];
        NSLog(@"%@",self.accountPath);
        finished(YES);
    }];

}
@end
